package com.unione.map.view.fragment

import android.view.LayoutInflater
import android.view.View
import android.view.View.GONE
import android.view.View.OnClickListener
import android.view.View.VISIBLE
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.impl.LoadingPopupView
import com.unione.map.R
import com.unione.map.databinding.FragmentEditMapStep1Binding
import com.unione.map.view.act.MainActivity
import com.unione.map.view.adapter.MapListAdapter
import com.unione.map.view.viewmodel.CreateMapStep1ViewModel
import com.unione.map.view.viewmodel.EditMapStep1ViewModel
import com.unione.unione_base.view.BaseViewModel
import com.unione.unione_protocol.UnioneRobotRepository
import com.unione.unione_protocol.robot_auto.bean.MapInfo
import kotlinx.coroutines.launch


class EditMapStep1Fragment : AppBaseFragment<FragmentEditMapStep1Binding>(), OnClickListener {

    private val viewModel: EditMapStep1ViewModel by viewModels()
    private lateinit var loadingPopupView: LoadingPopupView
    private var mLoadingPopup: BasePopupView? = null

    private lateinit var mAdapter: MapListAdapter
    private var mapList: MutableList<MapInfo> = mutableListOf()
    private lateinit var mSelectMap: MapInfo

    companion object {
        const val TAG = "EditMapStep1Fragment"

    }


    override fun onCreateViewBinding(
        inflater: LayoutInflater,
        parent: ViewGroup?
    ): FragmentEditMapStep1Binding {
        return FragmentEditMapStep1Binding.inflate(inflater, parent, false)
    }

    override fun init() {
        super.init()
        initView()
        initListeners()
        initData()
        initViewModelListeners()
    }


    private fun initData() {
        viewModel.getMapList()
    }


    private fun initView() {
        mAdapter = MapListAdapter()
        binding.rvList.layoutManager = LinearLayoutManager(requireContext())
        binding.rvList.adapter = mAdapter
        mAdapter.setList(mapList)
        mAdapter.setOnMapCheckedListener(object : MapListAdapter.OnMapCheckedListener {
            override fun onChecked(isChecked: Boolean, position: Int) {
                if (isChecked) {
                    for (item in mapList.withIndex()) {
                        if (item.index != position) {
                            item.value.isChecked = false
                        }
                    }
                    mapList[position].isChecked = true
                    mSelectMap = mapList[position]
                    mAdapter.setList(mapList)
                }
            }
        })
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            XPopup.Builder(requireContext()).asConfirm(
                "温馨提示",
                "确认删除此地图吗？",
                "取消",
                "删除",
                {

                },
                { },
                false
            ).show()
        }
    }


    private fun initListeners() {
        binding.clVoice.setOnClickListener(this)
        binding.clHome.setOnClickListener(this)
        binding.clBack.setOnClickListener(this)
        binding.btnNext.setOnClickListener(this)
    }

    private fun initViewModelListeners() {
        viewModel.liveDataMapList.observe(this) {
            mapList.clear()
            mapList.addAll(it)
            mAdapter.setList(mapList)
        }
    }


    override fun onClick(v: View?) {
        super.onClick(v)
        if (v == null) return
        val nvController =
            Navigation.findNavController(
                requireActivity() as MainActivity,
                R.id.fragment_container_view
            )
        when (v.id) {
            R.id.btnNext -> {
                if (!this::mSelectMap.isInitialized) {
                    ToastUtils.showLong("请选择地图")
                    return
                }
                nvController.navigate(R.id.editMapStep2Fragment)
            }
        }
    }

    override fun getViewModel(): BaseViewModel {
        return viewModel
    }
}